package com.ruoyi.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.system.dao.LargeScreenDao;
import com.ruoyi.system.domain.dto.QuaStatisticsDto;
import com.ruoyi.system.domain.vo.LargeScreenVo;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.LargeScreenService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class LargeScreenServiceImpl implements LargeScreenService {

    @Resource
    private LargeScreenDao largeScreenDao;
    @Resource
    private ISysConfigService sysConfigService;

    @Override
    public Integer queryNum() {
        //二级医院
        int ejgwNum = 0;
        try {
            ejgwNum = largeScreenDao.getEjgwNumByName();
        }catch (Exception e){
            log.error("查询二级医院数据异常");
        }
        //住院
        int zyNum = 0;
        try {
            //获取当年时间
            Date yearStart = DateUtil.beginOfYear(DateUtil.date());
            Date yearEnd = DateUtil.endOfYear(DateUtil.date());
            zyNum = largeScreenDao.queryNumByCrsj(yearStart, yearEnd);
        }catch (Exception e){
            log.error("查询住院数据异常");
        }
        //社区
        int sjNum = 0;
        try {
            //查询总数
            sjNum = largeScreenDao.getSqzTotalNum();
        }catch (Exception e){
            log.error("查询社区数据异常");
        }
        //住院+社区+二级医院
        return ejgwNum + zyNum + sjNum;
    }

    @Override
    public LargeScreenVo.Tysscrstj tysscrstj() {
        LargeScreenVo.Tysscrstj tysscrstj = new LargeScreenVo.Tysscrstj();
        //从配置信息中查询接入量
        String ejnum = sysConfigService.selectConfigByKey(UserConstants.SCREEN_EJNUM);
        try {
            tysscrstj.setEjnum(StrUtil.isEmpty(ejnum) ? 2 : Integer.parseInt(ejnum));
        }catch (Exception e){
            tysscrstj.setEjnum(2);
        }
//        //查询cdss的DiagnosisInfo
//        int ejgwNum = largeScreenDao.getEjgwNum();
        //查询健康山西的视图
        int ejgwNum = largeScreenDao.getEjgwNumByName();
        tysscrstj.setEjgw(ejgwNum);
        return tysscrstj;
    }

    @Override
    public LargeScreenVo.Tysscnldrstj tysscnldrstj() {
        //通过cdss下的DiagnosisInfo和患者表关联计算年龄分布
        return largeScreenDao.getEjgwNumGrouopAge();
    }

//    @Override
//    public LargeScreenVo.Eryybzfb eryybzfb() {
//        return largeScreenDao.getEryybzfb();
//    }

    @Override
    public LargeScreenVo.Tysczgltj tysczgltj() {
        //查询健康山西的视图
        LargeScreenVo.Tysczgltj tysczgltj = largeScreenDao.getSqnum();
        //社区医疗机构接入量
        //从配置信息中查询接入量
        String sqnum = sysConfigService.selectConfigByKey(UserConstants.SCREEN_HOSPITAL_NUM);
        try {
            tysczgltj.setSqnum(StrUtil.isEmpty(sqnum) ? 14 : Integer.parseInt(sqnum));
        }catch (Exception e){
            tysczgltj.setSqnum(14);
        }
        //查询总数
        int totalNum = largeScreenDao.getSqzTotalNum();
        tysczgltj.setSqz(totalNum);
        return tysczgltj;
    }

    @Override
    public LargeScreenVo.Tysczflzb tysczflzb() {
        //暂时不改
        return largeScreenDao.selectTysczflzb();
    }

    @Override
    public LargeScreenVo.Byzyrqfb byzyrqfb() {
//        LargeScreenVo.Byzyrqfb tyssczgltj = largeScreenDao.getEryybzfb();
        LargeScreenVo.Byzyrqfb tyssczgltj = largeScreenDao.getEryybzfbByName();
        return tyssczgltj;
    }

    @Override
    public LargeScreenVo.Bysjnwkzyrstj bysjnwkzyrstj() {
        //TODO 重新整理，查询拉取的数据，溶栓的用共建的
        //获取当前时间和24小时之前的时间
        Date now = DateUtil.date();
        Date yesterday = DateUtil.offsetHour(DateUtil.date(), -24);
        //获取当月时间
        Date monthStart = DateUtil.beginOfMonth(DateUtil.date());
        Date monthEnd = DateUtil.endOfMonth(DateUtil.date());
        //获取当年时间
        Date yearStart = DateUtil.beginOfYear(DateUtil.date());
        Date yearEnd = DateUtil.endOfYear(DateUtil.date());
        //查询数据
        LargeScreenVo.Bysjnwkzyrstj tysczgltj = new LargeScreenVo.Bysjnwkzyrstj();
        //24小时的数量
        tysczgltj.setToday(largeScreenDao.queryNumByCrsj(yesterday,now));
        //查询当月数据
        tysczgltj.setMonth(largeScreenDao.queryNumByCrsj(monthStart, monthEnd));
        //查询当年数据
        tysczgltj.setYear(largeScreenDao.queryNumByCrsj(yearStart, yearEnd));
        //查询全年介入
        tysczgltj.setJrzl(largeScreenDao.queryNumYearJr(yearStart, yearEnd, UserConstants.AISJRZT));
        //查询全年造影
        tysczgltj.setDsa(largeScreenDao.queryNumYearZy(yearStart, yearEnd));
        //查询溶栓数据
        tysczgltj.setRs(largeScreenDao.queryNumYearJmrs(yearStart, yearEnd));

        return tysczgltj;
    }

    @Override
    public LargeScreenVo.Bysjnwknldrstj tysscnldrstjBy() {
        //暂时不改
        LargeScreenVo.Bysjnwknldrstj tysscnldrstj = new LargeScreenVo.Bysjnwknldrstj();
        //太原市筛查年龄段人数统计
        List<Integer> ageList = largeScreenDao.getAge(null, null);
        if (CollectionUtil.isEmpty(ageList)) {
            return tysscnldrstj;
        }

        int num1 = 0;
        int num2 = 0;
        int num3 = 0;
        int num4 = 0;
        int num5 = 0;
        for (Integer integer : ageList) {
            if (integer < 40) {
                num1 += 1;
            }
            if (integer >= 40 && integer < 45) {
                num2 += 1;
            }
            if (integer >= 46 && integer <= 49) {
                num3 += 1;
            }
            if (integer >= 60 && integer <= 70) {
                num4 += 1;
            }
            if (integer > 70) {
                num5 += 1;
            }
        }
        tysscnldrstj.setNum1(num1);
        tysscnldrstj.setNum2(num2);
        tysscnldrstj.setNum3(num3);
        tysscnldrstj.setNum4(num4);
        tysscnldrstj.setNum5(num5);
        return tysscnldrstj;
    }

    @Override
    public LargeScreenVo.Gjzkwczzlfb gjzkwczzlfb() {
        Integer totalNum;
        int jmrsNum = largeScreenDao.queryNumYearJmrs(null, null);
        int ceaNum = largeScreenDao.queryBlsZlNumByCodes(null, null, UserConstants.CEA);
        int casNum = largeScreenDao.queryBlsZlNumByCodes(null, null, UserConstants.CAS);
        int aisjrztNum = largeScreenDao.queryBlsZlNumByCodes(null, null, UserConstants.AISJRZT);
        int ncxNum = largeScreenDao.queryBlsZlNumByCodes(null, null, UserConstants.NCX);
        int lndmlNum = largeScreenDao.queryBlsZlNumByCodes(null, null, UserConstants.LNDML);

        totalNum = jmrsNum + ceaNum + casNum + aisjrztNum + ncxNum + lndmlNum;

        LargeScreenVo.Gjzkwczzlfb gjzkwczzlfb = new LargeScreenVo.Gjzkwczzlfb();

        gjzkwczzlfb.setLndml(lndmlNum);
        gjzkwczzlfb.setJrzt(aisjrztNum);
        gjzkwczzlfb.setCea(ceaNum);
        gjzkwczzlfb.setCas(casNum);
        gjzkwczzlfb.setJmrs(jmrsNum);
        gjzkwczzlfb.setNcx(ncxNum);
        if (totalNum > 0) {
            gjzkwczzlfb.setCasBl(new BigDecimal(casNum).divide(new BigDecimal(totalNum), 2, RoundingMode.HALF_UP).multiply(new BigDecimal(100)));
            gjzkwczzlfb.setCeaBl(new BigDecimal(ceaNum).divide(new BigDecimal(totalNum), 2, RoundingMode.HALF_UP).multiply(new BigDecimal(100)));
            gjzkwczzlfb.setJmrsBl(new BigDecimal(jmrsNum).divide(new BigDecimal(totalNum), 2, RoundingMode.HALF_UP).multiply(new BigDecimal(100)));
            gjzkwczzlfb.setLndmlBl(new BigDecimal(lndmlNum).divide(new BigDecimal(totalNum), 2, RoundingMode.HALF_UP).multiply(new BigDecimal(100)));
            gjzkwczzlfb.setJrztBl(new BigDecimal(aisjrztNum).divide(new BigDecimal(totalNum), 2, RoundingMode.HALF_UP).multiply(new BigDecimal(100)));
            gjzkwczzlfb.setNcxBl(new BigDecimal(ncxNum).divide(new BigDecimal(totalNum), 2, RoundingMode.HALF_UP).multiply(new BigDecimal(100)));
        }
        return gjzkwczzlfb;
    }

    @Override
    public List<LargeScreenVo.getHospitalAndNum> getHospitalAndNum(QuaStatisticsDto.getHospitalAndNumByCityCode dto) {
        //通过code查找医院信息
        List<LargeScreenVo.getHospitalAndNum> hospitals = largeScreenDao.getOrgCodeByCityCode(dto);
        log.info("通过code查找医院信息:{}", hospitals.size());
        List<String> orgCodes = hospitals.stream().map(LargeScreenVo.getHospitalAndNum::getOrgCode).collect(Collectors.toList());
        //通过orgCode查找医院的筛查数据
        List<LargeScreenVo.getHospitalAndNum> hospitalAndNums = largeScreenDao.getHospitalAndNum(orgCodes);
        log.info("通过orgCode查找医院的筛查数据:{}", hospitalAndNums.size());
        Map<String, LargeScreenVo.getHospitalAndNum> hospitalAndNumMap = hospitalAndNums.stream().collect(Collectors.toMap(LargeScreenVo.getHospitalAndNum::getOrgCode, hospitalAndNum -> hospitalAndNum));
        for (LargeScreenVo.getHospitalAndNum hospital : hospitals) {
            LargeScreenVo.getHospitalAndNum getHospitalAndNum = hospitalAndNumMap.get(hospital.getOrgCode());
            if(getHospitalAndNum != null){
                hospital.setCount(getHospitalAndNum.getCount());
            }
        }
        for (LargeScreenVo.getHospitalAndNum hospital : hospitals){
            log.info(hospital.getHospitalName() + ":" + hospital.getCount());
        }
        return hospitals;
    }

    @Override
    public LargeScreenVo.Byczzxjbzlfb byczzxjbzlfb(QuaStatisticsDto.QueryPatient dto) {


        return largeScreenDao.byczzxjbzlfb(dto);
    }

}
